package org.dromara.swimming.controller;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.swimming.domain.bo.SwimmingBo;
import org.dromara.swimming.domain.vo.SwimmingVo;
import org.dromara.swimming.service.ISwimmingService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/swimming/plan")
public class SwimmingPlanController {

    private final ISwimmingService swimmingService;

    @GetMapping("/area")
    public R<Map> area(SwimmingBo bo) {
        List<SwimmingVo> swimmingVos = swimmingService.queryList(bo);

        Map<String, Object> map = new HashMap<>();
        ArrayList<Object> numbers = new ArrayList<>();
        ArrayList<Object> imgs = new ArrayList<>();
        ArrayList<Object> names = new ArrayList<>();
        for (SwimmingVo swimmingVo : swimmingVos) {
            if(swimmingVo.getSwimming()!=0&&swimmingVo.getReadySwimming()!=0){
                ArrayList<Object> number = new ArrayList<>();
                number.add(swimmingVo.getLatitude());
                number.add(swimmingVo.getLongitude());
                number.add(swimmingVo.getSwimming());
                numbers.add(number);
                imgs.add(swimmingVo.getAiImageUrl());
                names.add("观测点"+swimmingVo.getId().toString());
            }
        }
        map.put("numbers", numbers);
        map.put("imgs", imgs);
        map.put("names", names);
        return R.ok(map);
    }

    @GetMapping("/imgList")
    public R<List> imgList(SwimmingBo bo) {
        List<SwimmingVo> vos = swimmingService.queryList(bo);
        Collections.sort(vos, new Comparator<SwimmingVo>() {
            @Override
            public int compare(SwimmingVo vo1, SwimmingVo vo2) {
                return vo2.getUpdateTime().compareTo(vo1.getUpdateTime());
            }
        });
        ArrayList<SwimmingVo> list = new ArrayList<>();
        for(int i=0;i<(vos.size()<12?vos.size():12);i++){
            list.add(vos.get(i));
        }
        return R.ok(list);
    }

    @GetMapping("/list")
    public R<Map> list(SwimmingBo bo){
        List<SwimmingVo> vos = swimmingService.queryList(bo);
        Date nowDate = DateUtils.getNowDate();
        List<String> dateList = new ArrayList<>();
        for(int i=10;i>0;i--){
            Date date = DateUtils.addDays(nowDate, -1 * i);
            dateList.add(DateUtils.dateTime(date));
        }
        ArrayList<Object> swim = new ArrayList<>();
        ArrayList<Object> readyswim = new ArrayList<>();
        for(String day:dateList){
            int s=0;
            int ready=0;
            for(SwimmingVo vo:vos){
                String time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, vo.getUpdateTime());
                if(time.equals(day)){
                    s+=vo.getSwimming();
                    ready+=vo.getReadySwimming();
                }
            }
            swim.add(s);
            readyswim.add(ready);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("swimList",swim);
        map.put("readyList",readyswim);
        return R.ok(map);
    }

    @GetMapping("/total")
    public R<Map> total(SwimmingBo bo){
        List<SwimmingVo> vos = swimmingService.queryList(bo);
        Date nowDate = DateUtils.getNowDate();
        List<String> dateList = new ArrayList<>();
        for(int i=10;i>0;i--){
            Date date = DateUtils.addDays(nowDate, -1 * i);
            dateList.add(DateUtils.dateTime(date));
        }
        int swimtotal=0;
        int readytotal=0;
        for(String day:dateList){
            int s=0;
            int ready=0;
            for(SwimmingVo vo:vos){
                String time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, vo.getUpdateTime());
                if(time.equals(day)){
                    s+=vo.getSwimming();
                    ready+=vo.getReadySwimming();
                }
            }
            swimtotal+=s;
            readytotal+=ready;
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("swimTotal",swimtotal);
        map.put("readyTotal",readytotal);
        return R.ok(map);
    }

    @GetMapping("/digital")
    public R<Map> digital(SwimmingBo bo){
        List<SwimmingVo> vos = swimmingService.queryList(bo);
        Date nowDate = DateUtils.getNowDate();
        String now = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,nowDate);
        int swim=0;
        int ready=0;
        for(SwimmingVo vo:vos){
            String update = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, vo.getUpdateTime());
            if(update.equals(now)){
                swim+=vo.getSwimming();
                ready+=vo.getReadySwimming();
            }
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("swim",swim);
        map.put("ready",ready);
        return R.ok(map);
    }
}
